#define _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    struct comp
    {
        bool operator()(const pair<string, int>& p1, const pair<string, int>& p2)
        {
            return ((p1.second > p2.second) || (p1.second == p2.second && p1.first < p2.first));
        }
    };
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string, int> mp;
        for (auto& e : words)
        {
            mp[e]++;
        }
        vector<pair<string, int>> v(mp.begin(), mp.end());
        sort(v.begin(), v.end(), comp());
        vector<string> ret;

        for (int i = 0; i < k; i++)
        {
            ret.push_back(v[i].first);
        }
        return ret;
    }
};